package com.neuedu.javase.tools.str;

/***
 * 项目名称:  MyJavaSE 
 * 包：  com.neuedu.javase.tools.str 
 * date:   2025/10/30   10:46  
 * 描述:  
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class MyStringBuffer {

    public static void main(String[] args) {
        // 构造对象， 使用构造函数

        // 1 无参构造方法， 默认创造一个 16 长度的  容器  用于存放数据
        // StringBuffer buffer = new StringBuffer();
        // StringBuffer buffer = new StringBuffer(32);

        StringBuffer buffer = new StringBuffer("Hello String Buffer"); //  构造 的  长度有 内容决定(19)，  容器的长度     内容的length + 16 = 35

        System.out.println("数据的长度 buffer.length() = " + buffer.length());  // 19
        System.out.println("容器的长度 buffer.capacity() = " + buffer.capacity()); // 35


        //     可以动态的追加内容
        /**
         * AbstractStringBuilder(int capacity) {
         *         if (COMPACT_STRINGS) {
         *             value = new byte[capacity];
         *             coder = LATIN1;
         *         } else {
         *             value = StringUTF16.newBytesFor(capacity);
         *             coder = UTF16;
         *         }
         *     }
         */
        StringBuffer buffer2 = new StringBuffer(); // 容器默认16 byte value[16]


        // 通过append方法追加内容到 对象的末尾
        /**
         * public synchronized StringBuffer append(String str) {
         *         toStringCache = null;
         *         super.append(str);
         *         return this;
         *     }
         */
        StringBuffer buffer3 = buffer2.append("abc");
        buffer2.append(" def ");
        buffer2.append(" qqq ");
        System.out.println(buffer2);


        System.out.println("(buffer3 == buffer2) = " + (buffer3 == buffer2));


        System.out.println("=============================================insert===========================================");

        StringBuffer buffer4 = new StringBuffer("0123456789");
        System.out.println("插入之前 buffer4 = " + buffer4);
        System.out.println("插入之前 buffer4.capacity() = " + buffer4.capacity());
        buffer4.insert(5,"newStrDSFGHKERTYUVFNHJBNMRTYUNFNH");
        System.out.println("插入之后 buffer4 = " + buffer4);
        System.out.println("插入之后 buffer4.capacity() = " + buffer4.capacity());


        System.out.println("=============================================deleteCharAt===========================================");
        StringBuffer buffer5 = new StringBuffer("0123456789");
        System.out.println("buffer5 = " + buffer5);
        System.out.println("buffer5.deleteCharAt(4) = " + buffer5.deleteCharAt(4));


        System.out.println("=============================================delete===========================================");
        StringBuffer buffer6 = new StringBuffer("0123456789");
        System.out.println("buffer6 = " + buffer6);
        System.out.println("buffer6.delete(4,7) = " + buffer6.delete(4,7));   // [ 4, 7)


        //indexOf  lastIndexOf   replace  length()   charAt

        // codePointAt(int index)  返回指定索引处的字符（Unicode代码点
        System.out.println("=============================================codePointAt===========================================");
        StringBuffer buffer7 = new StringBuffer("ABCDEF");
        System.out.println("buffer7.codePointAt(0) = " + buffer7.codePointAt(0));   //A---Ansi 二进制 --> 十进制 65

        // reverse()
        System.out.println("=============================================reverse===========================================");
        System.out.println("buffer7 = " + buffer7);
        System.out.println("buffer7.reverse() = " + buffer7.reverse());


    }
}
